package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Enum.CementStatusEnum;
import com.itheima.Enum.CementTypeEnum;
import com.itheima.entity.Announcement;
import com.itheima.mapper.AnnouncementMapper;
import com.itheima.mapper.SysUserMapper;
import com.itheima.pojo.po.AnnouncementPo;
import com.itheima.query.AnnouncementPageQuery;
import com.itheima.result.PageResult;
import com.itheima.service.AnnouncementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guanhuaer
 * @since 2024-09-05
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    @Autowired
    private AnnouncementMapper cementMapper;
    @Autowired
    private SysUserMapper userMapper;

    @Override
    public PageResult cementPage(AnnouncementPageQuery cementPageQuery) {
        PageHelper.startPage(cementPageQuery.getPageNum(),cementPageQuery.getPageSize());
        Page<AnnouncementPo> page = cementMapper.cementPage(cementPageQuery);
        for (int i = 0; i < page.getResult().size(); i++) {
            page.getResult().get(i).setStatuss(CementStatusEnum.getStatus(page.getResult().get(i).getStatus()).getDesc());
            page.getResult().get(i).setCementType(CementTypeEnum.getType(page.getResult().get(i).getType()).getDesc());
        }
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void cementAdd(AnnouncementPo cementPo) {
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cementPo.setCreateTime(ft.format(dNow));
        cementPo.setStatus(CementStatusEnum.ENABLE.getStatus());
        cementMapper.insert(cementPo);
    }

    @Override
    public void cementAEdit(AnnouncementPo cementPo) {
        System.out.println(cementPo.getType());
        cementMapper.updateId(cementPo);
    }

    @Override
    public void Delects(String string) {
        String[] strings = string.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String item : strings) {
            cementMapper.Deldect(Integer.valueOf(item));
        }
    }


}
